#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/29 12:58 
# ide： PyCharm

from Models.assetModel import asset_model
from typing import List
from sqlalchemy.orm import Session
from Tools.database import engine
from Dependencies.db_depedencies import get_db
from Controllers.assetController import asset_controller
from Schemas.assetSchema import asset_schema
from fastapi import APIRouter, Depends, HTTPException

asset_model.Base.metadata.create_all(bind=engine)
router = APIRouter()


@router.get("/asset/get/list", response_model=List[asset_schema.Asset])
async def read_asset_list(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    asset_list = asset_controller.get_asset_by_id(db=db, skip=skip, limit=limit)
    return asset_list


@router.get("/asset/get/{asset_id}", response_model=asset_schema.Asset)
async def read_asset_by_list(asset_id: int, db: Session = Depends(get_db)):
    db_asset = asset_controller.get_asset_by_id(db, asset_id)
    if db_asset is None:
        raise HTTPException(status_code=404, detail="asset not found")
    return db_asset


@router.post("/asset/create", response_model=asset_schema.Asset)
async def create_asset(asset: asset_schema.AssetCreate, db: Session = Depends(get_db)):
    db_asset = asset_controller.create_asset(db=db, asset=asset)
    return db_asset
